import 'dart:convert';
import 'package:t2app/src/util/log_util.dart';
import 'package:t2app/src/util/sp_util.dart';

import '../Config.dart';

/**
 * 用户类
 */
class User{
   int id = 0;//id
   String user_sn = "";
   String nickname="匿名";//用户名
   String head_img= Config.LOGO;//头像
   String? expire_time;
   int plan_id = 0; //套餐id

   User(){
     //----
   }

   User.all(int id,String user_sn,String nickname,String head_img,String? expire_time,int plan_id){
     this.id = id;
     this.user_sn = user_sn;
     this.nickname = nickname;
     this.head_img = head_img;
     this.expire_time = expire_time;
     this.plan_id = plan_id;
   }

   /// jsonDecode(jsonStr)方法返回的是Map<String, dynamic>类型，需要这里将map转换成实体类
   static User fromMap(Map<String, dynamic> map) {
     User user = new User.all(map['id'],map["user_sn"],map["nickname"],map["head_img"], map["expire_time"], map['plan_id']);
     return user;
   }

   /// jsonDecode(jsonStr) 方法中会调用实体类的这个方法。如果实体类中没有这个方法，会报错。
   Map toJson() {
     Map map = new Map();
     map["id"] = this.id;
     map["name"] = this.nickname;
     map["user_sn"] = this.user_sn;
     map["head_img"] = this.head_img;
     map["expire_time"] = this.expire_time;
     map["plan_id"] = this.plan_id;
     return map;
   }

   bool isVip() {
     return this.plan_id!=0;
   }

   static User getUserByJsonString(String  str){
     return fromMap(jsonDecode(str));
   }

   Future<bool> putStorage(){
     String jsonStr = jsonEncode(this.toJson());
//     print("user转json:"+jsonStr);
     return SpUtil.instance.putString("user", jsonStr);
   }
}